package com.lili.price;

/**
 * @Auther: 李 力
 * @Date: 2024/8/26
 * @Description: com.lili.price
 * @version: 1.0
 */
public class LeetCode309 {

    public static int maxProfit(int[] prices) {
        if (prices.length == 1) {
            return 0;
        }
        int[] buy = new int[prices.length];
        int[] shell = new int[prices.length];
        buy[0] = -prices[0];
        shell[0] = 0;
        buy[1] = Math.max(-prices[0], -prices[1]);
        shell[1] = Math.max(shell[0], buy[0] + prices[1]);
        for (int i = 2; i < prices.length; i++) {
            buy[i] = Math.max(buy[i - 1], shell[i - 2] - prices[i]);
            shell[i] = Math.max(shell[i - 1], buy[i - 1] + prices[i]);
        }
        return shell[prices.length - 1];
    }
}
